/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RatingMatrix;

import DatabaseConnectivity.MySQLConnectivity;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Generating various user matrix
 * for list of ratings in 
 * array format.
 * @author Ekantik
 * @version 1.0
 */
public class ArrayRateMatrix {
    
    MySQLConnectivity msc;
    ArrayRateMatrix alrm;
    String query;
    static int userid =1;
     /**
     * for counting the number of users to participate in the votings
     * 18/04/2013
     */
    int count = 10;
    /**
     * For generating the number of users in the system
     * 18/04/2013
     */
    RandomNodes rn;
    
    /**
     * List of array to be stored in the form of user
     * 18/04/2013
     */
    ArrayList arrayList;
    
    /**
     * Here the number of matrix rows are being prepared.
     */
    int numberOfRows = 10;
    
    public static int getUserid() {
        return userid;
    }

    public static void setUserid(int userid) {
        ArrayRateMatrix.userid = userid;
    }
    
    public ArrayRateMatrix getAlrm() {
        return alrm;
    }

    public void setAlrm(ArrayRateMatrix alrm) {
        this.alrm = alrm;
    }
    public MySQLConnectivity getMsc() {
        return msc;
    }
    public void setMsc(MySQLConnectivity msc) {
        this.msc = msc;
    }
    
    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public ArrayList getArrayList() {
        return arrayList;
    }

    public void setArrayList(ArrayList arrayList) {
        this.arrayList = arrayList;
    }

    public int getNumberOfRows() {
        return numberOfRows;
    }

    public void setNumberOfRows(int numberOfRows) {
        this.numberOfRows = numberOfRows;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
    
    public RandomNodes getRn() {
        return rn;
    }

    public void setRn(RandomNodes rn) {
        this.rn = rn;
    }

    /**
     * Constructor for generating and populating the variables
     * 18/04/2013
     */
    public ArrayRateMatrix() {
        this.alrm = this;
        MySQLConnectivity m = new MySQLConnectivity();
        this.setMsc(m);
        this.arrayList = new ArrayList();
        populateArrayList();
    }
    /**
     * for populating the arrayList variable and defining
     * various levels of it.
     */
    public void populateArrayList(){
        //System.out.println(userid);
        for(int i=0;i<count;i++){
            for(int j = 0;j<count;j++){
                rn = new RandomNodes(j, 5);
                arrayList.add(rn);
            }
        }
    }
    public void print(String message){
        System.out.println(message);
    }
    private int noOfServices;
    private int noOfUsers;

    public int getNoOfUsers() {
        return noOfUsers;
    }

    public void setNoOfUsers(int noOfUsers) {
        this.noOfUsers = noOfUsers;
    }

    public int getNoOfServices() {
        return noOfServices;
    }

    public void setNoOfServices(int noOfServices) {
        this.noOfServices = noOfServices;
    }
    public void calculateNoOfServices(){
        String query = "select count(distinct sid) from ratings;";
        alrm.getMsc().getSelectTable(query);
        try{
            while(alrm.getMsc().getResultSet().next()){
                alrm.setNoOfServices(alrm.getMsc().getResultSet().getInt(1));
                //alrm.print("fromCalc:" + alrm.getMsc().getResultSet().getInt(1));
            }
        }
        catch(SQLException ex){
            
        }
    }
    public void calculateNoOfUsers(){
        String query = "select count(distinct username) from ratings;";
        alrm.getMsc().getSelectTable(query);
        try{
            while(alrm.getMsc().getResultSet().next()){
                alrm.setNoOfUsers(alrm.getMsc().getResultSet().getInt(1));
                //alrm.print("fromCalc:" + alrm.getMsc().getResultSet().getInt(1));
            }
        }
        catch(SQLException ex){
            
        }
    }
    public void calculateServicesAndUsers(){
        alrm.calculateNoOfServices();
        //alrm.print(" " +alrm.getNoOfServices());
        alrm.calculateNoOfUsers();
        //alrm.print(" " +alrm.getNoOfUsers());
    }
    private int[][] arrayMatrix;

    public int[][] getArrayMatrix() {
        return arrayMatrix;
    }

    public void setArrayMatrix(int[][] arrayMatrix) {
        this.arrayMatrix = arrayMatrix;
    }
    
    public void setArrayList(){
            this.getAlrm().calculateServicesAndUsers();
            this.getAlrm().setQuery("select votes from ratings order by uid,sid;");
            this.getAlrm().getMsc().getSelectTable(alrm.getQuery());
            arrayMatrix = new int[alrm.getNoOfServices()][alrm.getNoOfUsers()];
            try{

            this.getAlrm().getMsc().getResultSet().next();
            
            for(int i = 0;i<alrm.getNoOfUsers();i++){
                for(int j = 0;j<alrm.getNoOfServices();j++){
                    arrayMatrix[i][j] = this.getAlrm().getMsc().getResultSet().getInt("votes");
                    this.getAlrm().getMsc().getResultSet().next();
                }
            }
            }
            catch(SQLException ex){

            }
            /*for(int i=0;i<alrm.getNoOfServices();i++){
                for(int j = 0;j<alrm.getNoOfUsers();j++){
                    System.out.print(this.getArrayMatrix()[i][j]);
                }
                System.out.println();
            }*/
    }
    /**
     * Making main method for various tests.
     * @param args 
     */
    public static void main(String args[]){
        ArrayRateMatrix alrm = new ArrayRateMatrix();
        //alrm.insertQuery();
        alrm.setArrayList();
        alrm.getArrayMatrix();
    }
    
    public void insertQuery(){
        ArrayRateMatrix a = this.getAlrm();
        int sid = 0;
        for(int i = 0,j = 0;i<count*count;i++,j++){
                RandomNodes r = (RandomNodes) a.getArrayList().get(i);
                if(j % count == 0 && j !=0)
                    sid++;
                query = "insert into ratings (sid,uid,username,votes,participation) values";
                query += "("+sid+","+r.getId()+",'"+r.getName()+"',"+r.getVote()+","+r.getIsAlive()+");";
                alrm.getMsc().setQuery(query);
                alrm.getMsc().insertIntoTable();
        }
    }
}
